<!--车辆维修页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddVehicle"
               type="primary" style="float:right;margin-top:13px;">录入维修车辆
    </el-button>
  </div>
  <!-- 录入维修车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input placeholder="请输入" v-model="saveVehicleForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择车牌号" v-model.number="saveVehicleForm.vehicleId">
              <el-option v-for="vehicle in vehicleList" :key="vehicle.id" :label="vehicle.license"
                         :value="vehicle.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="saveVehicleForm.item">
              <el-option label="违章检查" value="10"></el-option>
              <el-option label="上线检测" value="20"></el-option>
              <el-option label="尾气检测" value="30"></el-option>
              <el-option label="外观检测" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="saveVehicleForm.addr">
              <el-option label="沃尔沃专修店" value="10"></el-option>
              <el-option label="美奥行汽修" value="20"></el-option>
              <el-option label="悦心金峰盛车检汽修" value="30"></el-option>
              <el-option label="京昌鑫顺汽修" value="30"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="saveVehicleForm.material">
              <el-option label="汽缸垫进排气歧管垫" value="10"></el-option>
              <el-option label="发动机前后悬挂胶垫" value="20"></el-option>
              <el-option label="板簧胶套" value="30"></el-option>
              <el-option label="消声器" value="40"></el-option>
              <el-option label="气门弹簧" value="50"></el-option>
              <el-option label="气门" value="60"></el-option>
              <el-option label="止推垫" value="70"></el-option>
              <el-option label="大小瓦" value="80"></el-option>
              <el-option label="四配套（缸套、活塞、活塞环、活塞销）" value="90"></el-option>
              <el-option label="轴承" value="100"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用（元）">
            <el-input placeholder="请输入" v-model="saveVehicleForm.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleForm.repairTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="saveVehicleForm.status">
              <el-radio label="维修中" value="1" style="margin:0;"></el-radio>
              <el-radio label="已完成" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">录入</el-button>
    </template>
  </el-dialog>
  <!-- 维修车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;" v-model="searchVehicleForm.license"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" style="width:290px;">
        <el-input placeholder="请输入送修人员姓名" style="width:220px;" v-model="searchVehicleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width: 290px;">
        <el-select placeholder="请选择" v-model="searchVehicleForm.status">
          <el-option label="维修中" value="1"></el-option>
          <el-option label="已完成" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 维修车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="vehicleId" label="维修车辆" align="center"
                       :formatter="vehicleIdFormatter"/>
      <el-table-column prop="item" label="维修项目" align="center"
                       :formatter="repairItemFormatter"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center"
                       :formatter="repairAddrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用（元）" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center"
                       :formatter="repairMaterialFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center"
                       :formatter="repairStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//控制录入车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('录入维修车辆');
//定义对象用来保存维修车辆的搜索条件
const searchVehicleForm = ref({license: '',username: '',status: ''})
//定义数组用来保存维修车辆表格数据
const vehicleArr = ref([]);
//定义一个编辑维修车辆的方法
const editVehicle = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑维修车辆';
  //根据当前id回显待编辑数据
  axios.get(BASE_URL + '/v1/repair/select?id=' + id).then((response) => {
    if (response.data.code === 2000) {
      //将后端返回的数据存入表单对象中
      saveVehicleForm.value = response.data.data[0];
      console.log("我是回显数据");
      console.log(saveVehicleForm.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
//定义加载维修车辆的方法
const loadVehicle = () => {
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL + '/v1/repair/select?' + data).then((response) => {
    console.log(response.data.data)
    if (response.data.code == 2000) {
      console.log(response.data.data);
      vehicleArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(() => {
  loadVehicle();
  loadVehicleList();
})
//定义重置搜索条件
const resetSearch = () => {
  searchVehicleForm.value = {};
  loadVehicle();
}

//车辆状态
const repairStatusFormatter = (row, column, cellValue, index) => {
  if (cellValue == 1) {
    cellValue = '维修中'
  } else if (cellValue == 0) {
    cellValue = '已完成'
  }
  return cellValue;
}
//维修材料
const repairMaterialFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '汽缸垫进排气歧管垫'
  } else if (cellValue == 20) {
    cellValue = '发动机前后悬挂胶垫'
  } else if (cellValue == 30) {
    cellValue = '板簧胶套'
  } else if (cellValue == 40) {
    cellValue = '消声器'
  } else if (cellValue == 50) {
    cellValue = '气门弹簧'
  } else if (cellValue == 60) {
    cellValue = '气门'
  } else if (cellValue == 70) {
    cellValue = '止推垫'
  } else if (cellValue == 80) {
    cellValue = '大小瓦'
  } else if (cellValue == 90) {
    cellValue = '四配套（缸套、活塞、活塞环、活塞销）'
  } else if (cellValue == 100) {
    cellValue = '轴承'
  }
  return cellValue;
}
//维修地点
const repairAddrFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '沃尔沃专修店'
  } else if (cellValue == 20) {
    cellValue = '美奥行汽修'
  } else if (cellValue == 30) {
    cellValue = '悦心金峰盛车检汽修'
  } else if (cellValue == 40) {
    cellValue = '京昌鑫顺汽修'
  }
  return cellValue;
}
//维修项目
const repairItemFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '违章检查'
  } else if (cellValue == 20) {
    cellValue = '上线检测'
  } else if (cellValue == 30) {
    cellValue = '尾气检测'
  } else if (cellValue == 40) {
    cellValue = '外观检测'
  }
  return cellValue;
}
// 定义车辆ID格式化函数
const vehicleIdFormatter = (row, column, cellValue, index) => {
  // 这里可以根据 vehicleId 查询车辆信息并返回车牌号或其他标识
  const vehicle = vehicleList.value.find(v => v.id === cellValue);
  return vehicle ? vehicle.license : cellValue;
}

//定义保存车辆表单数据的对象
const saveVehicleForm = ref({
  id: null,
  username: '',
  vehicleId: null,
  item: '',
  addr: '',
  material: '',
  fee: '',
  repairTime: '',
  status: ''
})

//在点击“录入车辆”按钮时调用
const beforeAddVehicle = () => {
  dialogTitle.value = '录入维修车辆';
  dialogVisible.value = true;
}
//定义保存车辆方法
const saveVehicle = () => {
  if (!saveVehicleForm.value.vehicleId) {
    ElMessage.error('请选择维修车辆！');
    return; // 阻止后续代码执行
  }
  if (!saveVehicleForm.value.username || saveVehicleForm.value.username.trim() == '') {
    ElMessage.error('请输入送修人');
    return;//阻止后续代码执行
  }
  if (!saveVehicleForm.value.repairTime) {
    ElMessage.error('请选择维修时间！');
    return;//阻止后续代码执行
  }
  console.log(saveVehicleForm.value);
  let data = qs.stringify(saveVehicleForm.value);

  // 发送POST请求
  axios.post(BASE_URL + '/v1/repair/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功');
      dialogVisible.value = false;//关闭弹窗
      saveVehicleForm.value = {};//清空表单
      loadVehicle();//重新加载数据
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义保存车辆表单弹窗关闭的方法
const handleClose = () => {
  if (confirm('你是否关闭弹窗')) {
    saveVehicleForm.value = {};
    dialogVisible.value = false;
  }
}
//删除车辆
const deleteVehicle = (id) => {
  if (confirm('你确定要删除吗？')) {
    axios.post(BASE_URL + '/v1/repair/delete/' + id).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功');
        loadVehicle();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

// 定义数组用来保存车辆列表
const vehicleList = ref([]);
// 加载车辆列表
const loadVehicleList = () => {
  axios.get(BASE_URL + '/v1/vehicle/select').then((response) => {
    if (response.data.code == 2000) {
      vehicleList.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  });
};
</script>
